/** utils about auth should be write here */
import Taro from "@tarojs/taro"
import { withStore } from "@/store/index"
import { receiveState } from "@/store/actions/auth"
import { wechatLogin, wechatLoginPath } from "@/apis/yikao-community"
import ls from "@/utils/ls"

/** 微信登录
 * @param { (fulfilled: boolean) => any } afterAuth 登录请求接口调用成功之后执行
 */
export const wxLogin = function (afterAuth?: (fulfilled: boolean) => any) {
  Taro.login({
    success(res) {
      if (res.code) {
        wechatLogin(res.code)
          .then(({ data }) => {
            if (data.success) {
              const { accessToken, ...userInfo } = data.data
              const store = withStore()
              store.dispatch(
                receiveState({
                  userInfo,
                  token: accessToken
                })
              )
              afterAuth && afterAuth(true)
            } else {
              Taro.showToast({
                title: data.message as string,
                icon: "none",
                mask: false
              })
              afterAuth && afterAuth(false)
            }
          })
          .catch(() => {
            afterAuth && afterAuth(false)
          })
      } else {
        Taro.showToast({
          title: "登录失败！" + res.errMsg,
          icon: "none",
          mask: false
        })
        afterAuth && afterAuth(false)
      }
    }
  })
}
/** 判断是否是登录接口 */
export const isLoginRequest = (reqUrl: string) => {
  return reqUrl.indexOf(wechatLoginPath) !== -1
}
export const authLs = {
  getToken(): IAuthStore["token"] {
    return ls.get("token", null)
  },
  getUserInfo(): IAuthStore["userInfo"] {
    return ls.get<IAuthStore["userInfo"]>("userInfo", {})
  },
  getExtra(): IAuthStore["extra"] {
    return ls.get<IAuthStore["extra"]>("extra", {
      chooseInfoIgnored: false,
      showAuthModal: false,
      authModalCanceled: false,
      authModalCancelFn: null,
      authModalConfirmFn: null
    })
  },
  setToken(token: IAuthStore["token"]) {
    ls.set("token", token)
  },
  setUserInfo(userInfo: IAuthStore["userInfo"]) {
    ls.set("userInfo", userInfo)
  },
  setExtra(_extra: IAuthStore["extra"]) {
    const shouldBeStore: IAuthStore["extra"] = {
      // chooseInfoIgnored: extra.chooseInfoIgnored
    }
    ls.set("extra", shouldBeStore)
  },
  removeToken() {
    ls.del("token")
  },
  removeUserInfo() {
    ls.del("userInfo")
  },
  removeExtra() {
    ls.del("extra")
  },
  removeAuthInfo() {
    authLs.removeToken()
    authLs.removeUserInfo()
    authLs.removeExtra()
  },
  patchUserInfo(userInfo: IAuthStore["userInfo"]) {
    const prev = authLs.getUserInfo()
    if (prev) {
      authLs.setUserInfo({
        ...prev,
        ...userInfo
      })
    } else {
      authLs.setUserInfo(userInfo)
    }
  },
  patchExtra(extra: IAuthStore["extra"]) {
    const prev = authLs.getExtra()
    if (prev) {
      authLs.setExtra({
        ...prev,
        ...extra
      })
    } else {
      authLs.setExtra(extra)
    }
  },
  setAuthInfo(authInfo: IAuthStore) {
    authLs.setToken(authInfo.token)
    authLs.setUserInfo(authInfo.userInfo)
  }
}
/** 判断是否需要进入选择信息页面 */
export function hasChosenInfo() {
  const { userInfo, extra } = withStore().getState().auth
  const ignored = Boolean(extra && extra.chooseInfoIgnored)
  // const hasSet = Boolean(userInfo.year && userInfo.areaId && userInfo.projectId)
  const hasSet = Boolean(userInfo.projectName && userInfo.projectName !== "综合")
  return {
    ignored,
    hasSet
  }
}
